package pdsgameserver;

import java.io.Serializable;

import com.sun.sgs.app.ManagedObject;
import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.DataManager;
import com.sun.sgs.app.ChannelManager;
import com.sun.sgs.app.Delivery;

public abstract class Game implements ManagedObject, Serializable
{
	protected String gameName;
	
	abstract void move(Player p, String move);	
	
	abstract boolean join(Player p);
	
	abstract void leave(Player p);
	
	public String getName()
	{
		return gameName;	
	}
	
	
	public void setName(String name)
	{
		DataManager dataManager = AppContext.getDataManager();
        dataManager.markForUpdate(this);
		ChannelManager channelMgr = AppContext.getChannelManager();
		channelMgr.createChannel(name, null, Delivery.RELIABLE);
		gameName = name;	
		
		
	}
	
	//Static Methods
	//==============================================================================
	public static Game newGame(String gameName, String gameType)
	{
		Game r = null;
		DataManager dataManager = AppContext.getDataManager();
		if(gameType.equals("chat"))
		{
			r = new ChatGame(gameName);
			dataManager.setBinding(gameName, r);
		}
		else if(gameType.equals("crazyeights"))
		{
			r = new CrazyEightsGame(gameName);
			dataManager.setBinding(gameName, r);
		}
		return r;
	}	

}
